package com.xxadmin.usercenter.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xxadmin.common.constant.Iconstant;
import com.xxadmin.common.dto.PageInfoDTO;
import com.xxadmin.common.exception.BusinessException;
import com.xxadmin.common.exception.errorcode.ErrorCode;
import com.xxadmin.common.util.clone.CopyUtils;
import com.xxadmin.common.util.page.PageUtils;
import com.xxadmin.usercenter.dao.MenuInfoDao;
import com.xxadmin.usercenter.domain.MenuInfo;
import com.xxadmin.usercenter.dto.MenuInfoDTO;
import com.xxadmin.usercenter.dto.MenuInfoQueryDTO;
import com.xxadmin.usercenter.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 菜单实现类
 *
 * @author:chenming
 * @date:2018/9/3
 */
@Service
public class MenuServiceImpl implements MenuService{

    @Autowired
    private MenuInfoDao menuInfoDao;

    @Override
    public void addMenuInfo(MenuInfoDTO menuInfoDTO) {

        if(menuIsExist(menuInfoDTO.getMenuCode())){
            throw new BusinessException(ErrorCode.IS_EXISTED,"该菜单编码已经存在，请勿重复添加");
        }

        if(menuIsExist(menuInfoDTO.getChannelCode(),menuInfoDTO.getMenuName())){
            throw new BusinessException(ErrorCode.IS_EXISTED,"当前渠道下已经存在该菜单，请勿重复添加");
        }

        MenuInfo menuInfo = CopyUtils.copyBean(menuInfoDTO,MenuInfo.class);

        menuInfo.setCreateTime(new Date());
        menuInfo.setStatus(Iconstant.STATUS_ENABLED);

        menuInfoDao.insertSelective(menuInfo);
    }

    private boolean menuIsExist(String channelCode, String menuName) {
        MenuInfo menuInfo = menuInfoDao.selectByChannelCodeAndName(channelCode,menuName);

        return menuInfo != null;
    }

    private boolean menuIsExist(String menuCode) {
        MenuInfo menuInfo = menuInfoDao.selectByCode(menuCode);

        return menuInfo != null;
    }

    @Override
    public void updateMenuInfo(MenuInfoDTO menuInfoDTO) {
        // 校验
        MenuInfo temp = menuInfoDao.selectByPrimaryKey(menuInfoDTO.getId());

        if(temp == null){
            throw new BusinessException(ErrorCode.NOT_FOUD,"修改的菜单不存在");
        }


        MenuInfo codeMenu = menuInfoDao.selectByCode(menuInfoDTO.getMenuCode());
        if(codeMenu != null && !codeMenu.getId().equals(menuInfoDTO.getId())){
            throw new BusinessException(ErrorCode.IS_EXISTED,"该菜单编码已经存在，请勿重复添加");
        }

        MenuInfo channelMenu = menuInfoDao.selectByChannelCodeAndName(menuInfoDTO.getChannelCode(),menuInfoDTO.getMenuName());
        if(channelMenu != null && !channelMenu.getId().equals(menuInfoDTO.getId())){
            throw new BusinessException(ErrorCode.IS_EXISTED,"当前渠道下已经存在该菜单，请勿重复添加");
        }


        MenuInfo menuInfo = CopyUtils.copyBean(menuInfoDTO,MenuInfo.class);

        menuInfo.setUpdateTime(new Date());

        menuInfoDao.updateByPrimaryKeySelective(menuInfo);
    }

    @Override
    public MenuInfoDTO getMenuInfo(Integer id) {
        MenuInfo menuInfo = menuInfoDao.selectByPrimaryKey(id);

        return CopyUtils.copyBean(menuInfo,MenuInfoDTO.class);
    }

    @Override
    public PageInfoDTO<MenuInfoDTO> listMenuInfo(MenuInfoQueryDTO queryDTO) {

        PageHelper.startPage(queryDTO.getPageNum(),queryDTO.getPageSize());

        Page<MenuInfo> page = menuInfoDao.selectMenuInfoList(queryDTO);

        return PageUtils.getPageInfoDTO(page,MenuInfoDTO.class);
    }
}
